﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using AutoClub.Core.Resources;

namespace AutoClub.Core.Domain
{
    public class Race : BaseEntity<Guid>
    {
        #region Properties

        [Required(AllowEmptyStrings = false, ErrorMessageResourceType = typeof(CoreResources), ErrorMessageResourceName = "RequiredFieldMessage")]
        [Display(Name = "StartDateField", ResourceType = typeof(CoreResources))]
        public DateTimeOffset StartDate { get; set; }

        [Display(Name = "EndDateField", ResourceType = typeof(CoreResources))]
        public DateTimeOffset? EndDate { get; set; }

        [Required(AllowEmptyStrings = false, ErrorMessageResourceType = typeof(CoreResources), ErrorMessageResourceName = "RequiredFieldMessage")]
        //[MinLength(1, ErrorMessageResourceType = typeof(CoreResources), ErrorMessageResourceName = "MinLenghtFieldMessage")]
        [Display(Name = "MechanicsMaxAmountField", ResourceType = typeof(CoreResources))]
        public int MechanicsMaxAmount { get; set; }

        [Required()]
        public RaceState State { get; set; }

        [Required(AllowEmptyStrings = false, ErrorMessageResourceType = typeof(CoreResources), ErrorMessageResourceName = "RequiredFieldMessage")]
        [Display(Name = "CategoryField", ResourceType = typeof(CoreResources))]
        public CarCategory Category { get; set; }

        internal IList<RaceMechanic> Mechanics { get; set; }

        #endregion

        #region Constructors

        public Race()
        {
            this.Mechanics = new List<RaceMechanic>();
            this.StartDate = DateTime.Today;
        }

        public Race(DateTimeOffset start, DateTimeOffset? end, int mechanicMaxAmount, RaceState state, CarCategory category)
            : this()
        {
            this.StartDate = start;
            this.EndDate = end;
            this.MechanicsMaxAmount = mechanicMaxAmount;
            this.State = state;
            this.Category = category;
        }


        #endregion

        #region Methods

        public string AddMechanic(Mechanic newMechanic)
        {
            RaceMechanic mechanicForThisRace = new RaceMechanic(this, newMechanic);
            this.Mechanics.Add(mechanicForThisRace);
            return mechanicForThisRace.GetNumber();
        }

        public IList<Mechanic> GetMechanics()
        {
            return this.Mechanics.Select(rm => rm.Mechanic).ToList();
        }

        #endregion
    }
}
